package com.work.xxzl.base;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.work.xxzl.R;
import com.work.xxzl.utils.StatusBarColorUtils;
import com.work.xxzl.view.LoadingDialog;

import butterknife.ButterKnife;


/**
 * Created by WZ on 2016/1/26
 * 基类Activity
 */
public abstract class BaseActivity extends AppCompatActivity implements
        OnClickListener {
    /**
     * 记录处于前台的Activity
     */
    private static BaseActivity mForegroundActivity = null;

    /**
     * 标题栏
     */
    protected TitleBar mTitleBar;
    //加载动画
    private LoadingDialog loadingDialog;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getRootView());

        //设置状态栏颜色
        StatusBarColorUtils.setColor(this, R.color.white);
//        //透明状态栏
//        StatusBarColorUtils.setTranslucentStatus(this);
        //去掉有些手机灰色蒙层
        StatusBarColorUtils.setTransparencyBar(this);
        //设置状态栏颜色
        StatusBarColorUtils.setStatusBarLightMode(this, StatusBarColorUtils.setStatusBarLightMode(this));
        //绑定ButterKnife
        ButterKnife.bind(this);
        init();
        initView();
        initData();
    }


    /**
     * 标题栏
     */
    public static class TitleBar {
        /**
         * 整个标题栏
         */
        public View mBar;// 标题栏
        public TextView titleMiddle;//标题
        public TextView titleRight;//编辑
        public RadioButton titleBack; // 返回按钮
        public ImageView titleImageRight; // 右侧按钮
    }

    /**
     * 返回界面布局
     */
    protected abstract int getLayoutId();

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 处理点击事件
     */
    protected void processClick(View v) {

    }

    /**
     * 获取根View
     */
    public View getRootView() {
        if (getLayoutId() != 0) {
            return View.inflate(this, getLayoutId(), null);
        } else {
            return View.inflate(this, R.layout.page_default, null);
        }
    }


    public void init() {
        mTitleBar = new TitleBar();
        mTitleBar.mBar = findViewById(R.id.common_title);
        mTitleBar.titleBack = findViewById(R.id.btn_back);
        mTitleBar.titleMiddle = findViewById(R.id.title_middle);
        mTitleBar.titleRight = findViewById(R.id.title_right);
        mTitleBar.titleImageRight = findViewById(R.id.title_image_right);
        mTitleBar.titleBack.setOnClickListener(this);
        mTitleBar.titleRight.setOnClickListener(this);
        mTitleBar.titleImageRight.setOnClickListener(this);
    }

    protected void onResume() {
        mForegroundActivity = this;
        super.onResume();
    }

    protected void onPause() {
        mForegroundActivity = null;
        super.onPause();
    }


    /**
     * 获取当前处于前台的activity
     */
    public static BaseActivity getForegroundActivity() {
        return mForegroundActivity;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                //返回
                finish();
                HideKeyboard(mTitleBar.mBar);
                break;
            //子类里面实现
//            case R.id.title_right:
//                UIUtils.showToast("编辑");
//                break;
//            case R.id.title_image_right:
//                UIUtils.showToast("搜索");
//                break;
            default:
                processClick(v);
                break;
        }
    }


    /***
     * 关软件盘
     *
     * @param v
     */
    public void HideKeyboard(View v) {
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) v.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);

            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        if (bgAlpha == 1) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,可能出现黑屏的bug
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        getWindow().setAttributes(lp);
    }

    /**
     * 设置加载提示
     */
    public void showLoaddialog() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        loadingDialog.setTitle("处理中");
        if (!loadingDialog.isShowing()) loadingDialog.show();
    }

    /**
     * 设置加载提示
     */
    public void showLoaddialog(String title) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        if (title != null) {
            loadingDialog.setTitle(title);
        } else {
            loadingDialog.setTitle("加载中");
        }
        if (!loadingDialog.isShowing()) loadingDialog.show();
    }

    /**
     * 设置提示框消失
     */
    public void hideLoaddialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) loadingDialog.dismiss();
    }


}
